package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.mapper.DeptMapper;
import com.ytjj.qmyx.admin.mapper.RoleMapper;
import com.ytjj.qmyx.admin.model.Dept;
import com.ytjj.qmyx.admin.model.Role;
import com.ytjj.qmyx.admin.model.request.DeptReq;
import com.ytjj.qmyx.admin.model.vo.DeptVO;
import com.ytjj.qmyx.admin.service.DeptService;
import com.ytjj.qmyx.admin.service.RoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class DeptServiceImpl implements DeptService {

    private DeptMapper deptMapper;

    private RoleService roleService;

    private RoleMapper roleMapper;

    @Override
    public CommonPage<DeptVO> deptLists(DeptReq deptReq) {
        PageHelper.startPage(deptReq.getPageNum(), deptReq.getPageSize());
        List<DeptVO> pageList = deptMapper.deptLists(deptReq.getName(),deptReq.getCompellation(),deptReq.getRoleName(),deptReq.getDeptId());
        return CommonPage.restPage(pageList);
    }

    @Override
    public List<DeptVO> deptList(Dept dept) {
        List<DeptVO> deptVOList= deptMapper.deptList(dept.getId());
        List<Role> roles = roleMapper.selectAll(dept.getRoleId());
        deptVOList.stream().forEach(d -> {
            d.setRoleList(roles.stream().filter(role -> null != role.getDepartmentId() && role.getDepartmentId().equals(d.getId())).collect(Collectors.toList()));
        });
        return deptVOList;
    }

    @Override
    public List<Role> roleList(Integer id) {
        return deptMapper.roleList(id);
    }

    @Override
    public int insertDept(String deptName) {
        return deptMapper.insertDept(deptName);
    }

    @Override
    public int updateDept(Dept dept) {
        return deptMapper.updateDept(dept);
    }

    @Override
    public Dept selectDept(Integer id) {
        return deptMapper.selectDept(id);
    }

    @Override
    public int deleteDept(Integer id) {
        return deptMapper.deleteDept(id);
    }

    @Override
    public int adminList(Integer id) {
        return deptMapper.adminList(id);
    }

    @Override
    public List<Role> findRoleByDeptId(Integer id) {
        return deptMapper.findRoleByDeptId(id);
    }

    @Override
    public Integer findByDeptName(String deptName) {
        return deptMapper.findByDeptName(deptName);
    }

    @Override
    public int updateRoleByAdminId(Integer roleId,Integer adminId) {
        return deptMapper.updateRoleByAdminId(roleId,adminId);
    }
}
